package com.hubu.utils;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 *
 * Nio文件读写工具类
 */
public class NioFileUtils {
    public static void copyFile(String srcPath,String destPath){
        File srcFile=new File(srcPath);
        //检验源文件
        if(!srcFile.exists()||!srcFile.isFile()||!srcFile.canRead()){
            throw new RuntimeException("目标文件"+srcFile+"无法操作");
        }
        File destFile=new File(destPath);
        //如果父级目录不存在，尝试创建出来
        String parent = destFile.getParent();
        File parentFile=new File(parent);
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        //判断当前目录是否合法
        if(destFile.exists()&&destFile.isDirectory()){
            throw new RuntimeException("拷贝位置是目录,无法拷贝");
        }
        copyFile(srcFile,destFile);
    }
    private static void copyFile(File srcFile,File destFile) {
        String parent = destFile.getParent();
        File parentFile=new File(parent);
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        //判断当前目录是否合法
        if(destFile.exists()&&destFile.isDirectory()){
            throw new RuntimeException("拷贝位置是目录,无法拷贝");
        }
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        ByteBuffer buffer=ByteBuffer.allocate(1024*1024);
        FileChannel inChannel=null;
        FileChannel outChannel=null;
        try {
            fileInputStream=new FileInputStream(srcFile);
            fileOutputStream =new FileOutputStream(destFile);
            inChannel = fileInputStream.getChannel();
            outChannel = fileOutputStream.getChannel();
            while((inChannel.read(buffer))!=-1){
                //切换读取模式
                buffer.flip();
                outChannel.write(buffer);
                buffer.clear();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(inChannel!=null) inChannel.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
            if(outChannel!=null) try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(fileInputStream!=null) try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(fileOutputStream!=null) try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static  void copyDirectory(String srcPath,String destPath){
        File srcFile=new File(srcPath);
        //检验源文件
        if(!srcFile.exists()||srcFile.isFile()||!srcFile.canRead()){
            throw new RuntimeException("目标文件"+srcFile+"无法操作");
        }
        int index = destPath.indexOf(srcPath);
        if(index!=-1){
            System.out.println("目标目录包含源目录");
        }
        File destFile=new File(destPath);
        if(destFile.exists()&&destFile.isFile()){
            throw new RuntimeException("拷贝位置是文件,无法拷贝");
        }

        copyDirectory(srcFile,destFile,destPath);
    }
    private static void copyDirectory(File srcFile,File destFile,String destPath) {
        File[] files = srcFile.listFiles();
        if (files != null && files.length > 0) {
            for (File item : files) {
                if (item.isDirectory()) {
                    if(item.getAbsolutePath().equals(destPath)){
                        continue;
                    }
                    //如果当前文件是目录，先将目录创建出来
                    File currentDestFile = new File(destFile, item.getName());

                    if (!currentDestFile.exists()) {
                        currentDestFile.mkdirs();
                    }
                    copyDirectory(item, currentDestFile,destPath);
                } else if (item.isFile()) {
                    copyFile(item, new File(destFile, item.getName()));
                }
            }
        }
    }
}
